package cn.itcast.action;


import com.opensymphony.xwork2.ActionContext;
import domain.page.PageBean;
import domain.take_delivery.Promotion;
import domain.utils.Constants;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/21/021.
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Actions
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion>{
    @Action(value = "promotion_pageQuery",results = {
            @Result(name = "success",type = "json")
    })
    public String pageQuery(){
        PageBean pageBean = WebClient.create(Constants.BOS_MANAGEMENT_URL + "/bos_management/services/promotionService/pageQuery?page=" +
                page + "&" + "rows=" + rows).accept(MediaType.APPLICATION_JSON).get(PageBean.class);
        ActionContext.getContext().getValueStack().push(pageBean);
        return SUCCESS;
    }


    @Action(value = "promotion_showDetail")
    public String showDetail() throws IOException, TemplateException {
        //生成宣传访问路径
        String htmlRealPath = ServletActionContext.getServletContext().getRealPath("/freemarker");
        File htmlfile = new File(htmlRealPath+"/"+model.getId()+".html");
        if(!htmlfile.exists()){
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
            configuration.setDirectoryForTemplateLoading(new File(ServletActionContext.getServletContext().getRealPath(
                    "/WEB-INF/freemarker_templates"
            )));
            configuration.setEncoding(Locale.CHINA,"UTF-8");
            Template template = configuration.getTemplate("promotion_detail.ftl");
            //获取动态数据
            Promotion promotion = WebClient.create(Constants.BOS_MANAGEMENT_URL + "/bos_management/services/promotionService/promotion/" +
                    model.getId()).accept(MediaType.APPLICATION_JSON).get(Promotion.class);
            Map<String,Object> promotionMap = new HashMap<>();
            promotionMap.put("promotion",promotion);
            //合并模板和实体对象输出
            template.process(promotionMap,new OutputStreamWriter(new FileOutputStream(htmlfile),"UTF-8"));
        }
            //存在则直接返回文件
            ServletActionContext.getResponse().setContentType("UTF-8");
            //输出文件到前台
            FileUtils.copyFile(htmlfile,ServletActionContext.getResponse().getOutputStream());

        return NONE;
    }
}
